package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.OrderCreateDTO;
import com.example.entity.OrderItem;
import com.example.entity.Sku;
import com.example.entity.SkuVo;
import com.example.exception.NoStockException;
import com.example.mapper.SkuMapper;
import com.example.service.SkuService;
import com.example.vo.SkusAttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @author 12057
* @description 针对表【sku(sku信息)】的数据库操作Service实现
* @createDate 2024-05-13 13:32:24
*/
@Service
@Slf4j
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku>
    implements SkuService{
    @Override
    public List<SkuVo> getSkusBySpuId(Integer spuId) {

        return baseMapper.getSkusBySpuId(spuId);
    }

    @Override
    public SkuVo getSkuBySkuId(Integer skuId) {
        return baseMapper.getSkuBySkuId(skuId);
    }

    @Override
    public List<SkusAttrVo> getAllSkuSaleAttrBySkuIds(List<Long> skuIds) {
        return baseMapper.getAllSkuSaleAttrBySkuIds(skuIds);
    }

    @Override
    public BigDecimal getMinPrice(Integer spuId) {
        return baseMapper.getMinPrice(spuId);
    }

    @Override
    public String getDefaultImageBySpuId(Integer spuId) {
        return baseMapper.getDefaultImageBySpuId(spuId);
    }

    @Override
    public List<String> getSkuImagesById(Long skuId) {
        return baseMapper.getSkuImagesById(skuId);
    }


    @Override
    public List<Sku> getGoodsBySkuIds(List<Long> ids) {
        // 如果ID列表为空，则返回空列表
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }

        // 创建一个LambdaQueryWrapper用于构建查询条件
        LambdaQueryWrapper<Sku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Sku::getId, ids);

        // 执行查询
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> getAllSkusImagesById(List<Long> skuIds) {
        return baseMapper.getAllSkusImagesById(skuIds);
    }

    @Override
    public List<SkuVo> getExamSkusBySpuId(Integer spuId) {
        return baseMapper.getExamSkusBySpuId(spuId);
    }

    @Override
    public List<String> getExamSkuImagesById(Long skuId) {
        return baseMapper.getExamSkuImagesById(skuId);
    }

    @Override
    public List<SkusAttrVo> getExamAllSkuSaleAttrBySkuIds(List<Long> skuIds) {
        return baseMapper.getExamAllSkuSaleAttrBySkuIds(skuIds);
    }

    @Override
    public List<String> getExamAllSkusImagesById(List<Long> skuIds) {
        return baseMapper.getExamAllSkusImagesById(skuIds);
    }


    @Override
    @Transactional
    public void lockStock(Long skuId, Integer skuQuantity) throws NoStockException {
        Sku sku = baseMapper.selectById(skuId);
        if (sku == null) {
            throw new NoStockException("商品不存在");
        }

        if (sku.getStock() < skuQuantity) {
            throw new NoStockException(skuId);
        }

        int updatedRows = baseMapper.lockStock(skuId, skuQuantity);
        if (updatedRows == 0) {
            throw new NoStockException(skuId);
        }
    }


    @Transactional
    @Override
    public void unlockStock(List<OrderCreateDTO> orders) {
        List<OrderItem> orderItems = new ArrayList<>();

        for (OrderCreateDTO orderCreateDTO : orders) {
            orderItems.addAll(orderCreateDTO.getOrderItems());
        }
        for (OrderItem orderItem : orderItems) {
            int updatedRows = baseMapper.unlockStock(orderItem.getSkuId(), orderItem.getSkuQuantity());
            if (updatedRows == 0) {
                log.error("库存解锁失败，商品ID: {}", orderItem.getSkuId());
            }
        }
    }
    //卖
    @Override
    public int updateSkuStock(Long skuId, Long stock) {
        return baseMapper.updateStock(skuId, stock);
    }
}




